package com.ruoyi.odorservice.controller;

import com.ruoyi.common.core.domain.Result;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.odorservice.entity.Odor;
import com.ruoyi.odorservice.mapper.OdorMapper;
import com.ruoyi.odorservice.model.bean.OdorInfoBean;
import com.ruoyi.odorservice.model.vo.OdorMaterialVo;
import com.ruoyi.odorservice.service.OdorService;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/odor")
@Api(tags = "气味信息")
@AllArgsConstructor
public class OdorController {

    private OdorService odorService;
    private OdorMapper odorMapper;

    @GetMapping("/getOdorList")
    @ApiOperation(value = "获取气味名称列表",httpMethod = "GET", response = Odor.class)
    public Result<List<OdorMaterialVo>> getOdorList(){
        List<OdorMaterialVo> odorMaterialVo = odorService.getOdorList();
        return Result.ok(odorMaterialVo);
    }

    @PostMapping("/createOdor")
    @ApiOperation(value = "新增气味",httpMethod = "POST", response = Boolean.class)
    public Result<Boolean> createOdor(@RequestBody OdorInfoBean odorInfoBean){
        if(odorInfoBean == null){
            return Result.fail("气味信息不能为空");
        }
        if(StringUtils.isNull(odorInfoBean.getOdorName()) || StringUtils.isEmpty(odorInfoBean.getOdorName())){
            return Result.fail("气味名称不能为空");
        }
        Odor odor = odorMapper.selectOdorByOdorName(odorInfoBean.getOdorName());
        if(odor != null){
            return Result.fail("气味已存在");
        }
        Boolean createState = odorService.createOdor(odorInfoBean);
        return Result.ok(createState);
    }

    @PutMapping("/updateOdor")
    @ApiOperation(value = "修改气味",httpMethod = "PUT", response = Boolean.class)
    public Result<Boolean> updateOdor(@RequestBody OdorInfoBean odorInfoBean){
        if(odorInfoBean == null){
            return Result.fail("气味信息不能为空");
        }
        if(StringUtils.isNull(odorInfoBean.getOdorName()) || StringUtils.isEmpty(odorInfoBean.getOdorName())){
            return Result.fail("气味名称不能为空");
        }
        if(StringUtils.isNull(odorInfoBean.getOdorId())){
            Result.fail("气味id不能为空");
        }
        Odor odor = odorMapper.selectOdorByOdorName(odorInfoBean.getOdorName());
        if(odor != null && !Objects.equals(odor.getId(), odorInfoBean.getOdorId())){
            return Result.fail("气味已存在");
        }
        Boolean updateState = odorService.updateOdor(odorInfoBean);
        return Result.ok(updateState);
    }

    @DeleteMapping("/deleteOdor")
    @ApiOperation(value = "删除气味",httpMethod = "DELETE", response = Boolean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "气味id", paramType = "query", dataType = "Integer", required = true),
    })
    public Result<Boolean> deleteOdor(@RequestParam(value = "id") Integer id){
        if(StringUtils.isNull(id)){
            Result.fail("气味id不能为空");
        }
        Boolean deleteState = odorService.deleteOdor(id);
        return Result.ok(deleteState);
    }
}
